//
//  LoginQuicklyController.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/8/9.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginQuicklyController.h"
//#import "TalkingData.h"
#import "EncryptAction.h"

@interface LoginQuicklyController () <LoginNormalCellDelegate>

@end

@implementation LoginQuicklyController


#pragma  mark - init
- (instancetype)init{
    if (self = [super init]) {
        self.configureStyle = kLocalConfigQuick;

    }
    return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
  self.title = @"验证手机号";
  [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
  self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.loginMainView hiddenExtraViewWith:@"确定"];
  
//  [TalkingData trackEvent:@"进入手机注册页面" label:[APP_NO stringValue]];
}

- (void)backBtnPressed:(id) sender{
  [self dismissViewControllerAnimated:YES completion:^{
    
  }];
    //[self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark -
#pragma mark tableview datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSArray *array = [LoginMainAction getLocalConfig:self.configureStyle];
    return [array count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LoginNormalCell *cell = [LoginNormalCell loginNormalCell:tableView];
    cell.configDic = [[LoginMainAction getLocalConfig:self.configureStyle] objectAtIndex:indexPath.section];
    cell.userInput.delegate = self;
    cell.loginDelegate = self;
    cell.layoutTouserInput = self.configureStyle;
    if (indexPath.section == 1) {
        cell.userInput.secureTextEntry = NO;
    }
    [cell.getVerify addTarget:self action:@selector(verifyBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}


-(void)loginBtnPressed:(id)sender {
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * phone = cell.userInput.text;
    
    indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * verifyCode = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"手机号不能为空"];
        return;
    }else if (verifyCode.length <= 0) {
        [self showToastAddWithTitle:@"验证码不能为空"];
        return;
    }
    
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction quickLogin:phone withCode:verifyCode withCallBlock:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
            
            [self showToastAddWithTitle:error_msg];
            
        }else {
          
//          [TalkingData trackEvent:@"手机登录成功" label:[APP_NO stringValue]];
          [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
            if (goBack) {
                [loginMainAction gorGoBackToMeViewController];
            }
          if (self.isRn) {
              UIApplication *application = [UIApplication sharedApplication];
              UIWindow *window = application.keyWindow;
              
              
              
              UIViewController * rootViewC= window.rootViewController;
              [rootViewC.navigationController popViewControllerAnimated:YES];
//            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
////            [appDelegate.preController.navigationController popViewControllerAnimated:YES];
//            [appDelegate.preController.navigationController popToRootViewControllerAnimated:YES];

          }else{
            [self dismissViewControllerAnimated:YES completion:nil];
          }
        }
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    UITextField * userName = cell.userInput;
    indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    UITextField * passWord = cell.userInput;
    
    if (textField == userName) {
        if ([passWord.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
           
            [self.loginMainView initLoginBtn];
        }else {
            [self.loginMainView activeLoginBtn];
        }
    }else if (textField == passWord) {
        if ([userName.text isEqualToString:@""]||(range.location == 0 && [string isEqualToString:@""])) {
            
            [self.loginMainView initLoginBtn];
        }else {
            [self.loginMainView activeLoginBtn];
        }
        
    }else{
        
    }
    return  YES;
}
- (void)clearContent:(LoginNormalCell *)cell{
    [loginMainView initLoginBtn];
}





@end
